﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace BF3RconSimulator
{
    static class Extensions
    {
        internal static bool CaseInsensitiveEquals(this string str, params string[] str2)
        {
            foreach (var s in str2)
            {
                if (string.Equals(str, s, StringComparison.OrdinalIgnoreCase)) return true;
            }
            return false;
        }

        //returns null if conversion fails due to format
        //overflows will be 
        internal static int? ToIntegerInvariant(this string str)
        {
            int valueInt; //this one is int, not uint
            try
            {
                valueInt = int.Parse(str, CultureInfo.InvariantCulture);
            }
            catch (OverflowException)
            {
                valueInt = -1;
            }
            catch (FormatException)
            {
                //see if it's a float instead
                double valueDouble;
                try
                {
                    if (double.TryParse(str, NumberStyles., CultureInfo.InvariantCulture, out valueDouble))
                        valueInt = Convert.ToInt32(valueDouble);
                }
                catch (OverflowException)
                {
                    //TODO: for numeric range, check to see if double when int
                    throw;
                }
                return Response.Fail("InvalidValue");
            }     
        }
    }
}
